﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using VsxFactory.Modeling.Repository;
using VsxFactory.Modeling.VisualStudio;

namespace DependenciesManager.Tests
{
    /// <summary>
    ///This is a test class for RepositoryCacheTest and is intended
    ///to contain all RepositoryCacheTest Unit Tests
    ///</summary>
    [TestClass()]
    public class RepositoryCacheTest
    {
        private TestContext testContextInstance;
        private static string baseFolder;
        private static PackageInfo packageInfo1;

        class TestServiceProvider : IServiceProvider
        {
            public object GetService(Type serviceType)
            {
                return null;
            }
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            baseFolder = Utils.GetTemporaryFolder();
            packageInfo1 = new PackageInfo() { Version = "1,0,0,0", Name = "Test 1", Kind=PackageKind.Strategy, Description="Test" };
        }

        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            Utils.RemoveDirectory(baseFolder);
        }

        private RepositoryDataContext CreateDataContext()
        {
            var dc = new RepositoryDataContext(new Uri("http://localhost:2929/Strategies.svc"));
            dc.Credentials = System.Net.CredentialCache.DefaultCredentials;
            return dc;
        }

        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void TestAddPackage()
        {
            var dataContext = CreateDataContext();
            var ms = new FileStream(@"D:\Profils\All Users\Application Data\VsxFactory\Strategies\strategies.pkx", FileMode.Open);
            dataContext.AddToPackages(packageInfo1);
            dataContext.SetSaveStream(packageInfo1, ms);
            dataContext.SaveChanges();
            var list = dataContext.Packages.ToList();
            Assert.IsTrue(list.First().ID == "1");
        }

        [TestMethod()]
        public void TestDownloadPackage()
        {
            var dataContext = CreateDataContext();
            var pi = dataContext.Packages.Where(p => p.ID == packageInfo1.ID && p.Version == packageInfo1.Version).First();
            using (var response = dataContext.GetReadStream(pi, "application/octet-stream"))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    response.Stream.CopyTo(ms);
                    Assert.IsTrue(ms.Length == 42507);
                }
            }
        }

        [TestMethod()]
        public void TestUpdatePackage()
        {
            var dataContext = CreateDataContext();
            string str = String.Format("Updating test {0}", DateTime.Now.ToLongTimeString());
            packageInfo1.Description = str;
            dataContext.AttachTo("Packages", packageInfo1);
            dataContext.UpdateObject(packageInfo1);
            dataContext.SaveChanges();

            dataContext = CreateDataContext();
            var list = dataContext.Packages.ToList();
            Assert.IsTrue(list.Count == 1);
            Assert.AreEqual(list[0].Description, str);
        }

        [TestMethod()]
        public void TestRemovePackage()
        {
            var dataContext = CreateDataContext();
            dataContext.AttachTo("Packages", packageInfo1);
            dataContext.DeleteObject(packageInfo1);
            dataContext.SaveChanges();
            var list = dataContext.Packages.ToList();
            Assert.IsTrue(list.Count == 0);
        }

    }
}
